<?php

class AdminStudent_Controller extends Admin_Controller {
    public function index(Request $request, Response $response) {
        $size = intval($request->size) ?: 30;
        $p = intval($request->p ?: 1);
        $from = ($p - 1) * $size;
        $q = new TrueQuery();
        if ($request->search) $q = new OrQuery(new SearchQuery('name', $request->search), new SearchQuery('studentNo', $request->search));
        $data = Student::find($q, ['from' => $from, 'size' => $size, 'sort' => ['created' => 'ASC']]);
        $page_nav['count'] = Student::totalCount($q);
        $nav = new SubPages($size, $page_nav['count'], $p, 10, "/admin/student/?p=", 2);
        $page_nav['nav'] = $nav->subPageCss2();
        $response->append(new View('admin/student/list', compact('data', 'page_nav')));
    }

    public function edit(Request $request, Response $response) {
        if (!$request->isPost()) {
            $id = $request->path(3);
            $data = Student::load($id);
            if (!$id || !$data) $data = new Student();
            $response->send(new View('admin/student/edit', compact('data')));
        }
        /**
         * @var	Student	$data
         */
        $originalNo = Student::load($request->id) ? Student::load($request->id)->studentNo : '';
        $data = Student::createByRequest($request);
        $nullCheck = [
            'studentNo' => '学号',
            'name' => '学生姓名',
            'sex' => '性别',
            'classId' => '所属班级',
        ];
        foreach ($nullCheck as $item => $description) if (!$data->$item) $response->alertErrorAndGoBack("{$description}不能为空！");
        $isNew = $data->id ? false : true;
        if ($isNew) {
            $duplicateCheck = [
                'studentNo' => '学号',
            ];
            foreach ($duplicateCheck as $item => $description) if (Student::totalCount(new Query($item, $data->$item)) != 0) $response->alertErrorAndGoBack("已有{$item}为{$description}的记录存在!不允许重复!");
        }
        $data->save();
        if ($isNew) {
            $user = new User();
            $user->internalId = $data->id;
            $user->name = $data->studentNo;
            $user->password = '';
            $user->type = User::TYPE_STUDENT;
            $user->save();
            $user->password = $user->generatePassword($data->studentNo);
            $user->save();
            $response->alertAndGoTo('操作成功!默认登陆用户名和密码均为学号!', '/admin/student/');
        }
        if ($isNew && $originalNo != $data->studentNo) {
            $user = array_values(User::find(new Query('name', $originalNo)))[0];
            $user->name = $data->studentNo;
            $user->save();
            $response->alertAndGoTo('操作成功!登陆用户名已修改为新学号!', '/admin/student/');
        }
        $response->alertAndGoTo('操作成功!', '/admin/student/');
    }

    public function delete(Request $request, Response $response) {
        if (!$request->path(3)) $response->alertErrorAndGoBack('请求无效！');
        /**
         * @var Student $student
         */
        if (!($student = Student::load($request->path(3)))) $response->alertAndGoTo('该记录不存在！', '/admin/student/');
        $student->remove();
        $response->alertAndGoTo('删除成功!所有相关记录已删除!', '/admin/student/');
    }
}